Skip to content

Vectorize vec_topetsc and petsc_to_psydac - #592

Open
max-models wants to merge 14 commits into
develfrom
topetsc-vectorized
Open

max-models wants to merge 14 commits into
develfrom
topetsc-vectorized

Conversation

@max-models

@max-models max-models commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Converting vectors between PSYDAC and PETSc used to loop over every entry in Python, and each entry needed a search for the process that owns it. This PR replaces both directions with NumPy array operations:

  • vec_topetsc: each process already knows the PETSc offset of its own data, so it gathers its non-zero entries with np.nonzero and inserts them all with a single setValues call.
  • petsc_to_psydac: the local PETSc array is written straight into the interior of the stencil data, instead of being read back with one getValue call per entry.

This also fixes a bug in petsc_to_psydac with the argument out: before, only the non-zero entries of the PETSc vector were copied, so out kept its previous values wherever the PETSc vector was zero (fixes #640). Now all the local entries are copied.

The output is the same as before, except for the fix of #640. The existing serial and parallel topetsc tests pass (stencil vectors in 1D, 2D and 3D, plus block vectors).

Speedup of vec_topetsc on a 2D vector:

❯ python psydac/linalg/tests/bench_topetsc.py
     n1 x n2   local size      old [s]      new [s]    speedup   check
   20x20              400      0.00484      0.00005     103.6x   OK
   40x40             1600      0.01973      0.00006     304.9x   OK
   80x80             6400      0.08463      0.00013     657.7x   OK
  160x160           25600      0.38283      0.00036    1073.1x   OK
  320x320          102400      1.97321      0.00131    1509.5x   OK

TODO

  • Add a regression test in psydac/linalg/tests/test_stencil_vector.py with two StencilVector objects: x with some zero entries, and out pre-filled with non-zero values; check that out equals x after the roundtrip with vec_topetsc and petsc_to_psydac. The test should fail on devel, as the reproducer of petsc_to_psydac keeps stale values in out where the PETSc vector is zero #640 does.
  • Add another regression test in psydac/linalg/tests/test_block.py: the same as above, but for a BlockVector of two StencilVector blocks of different sizes (for example 6 and 4).
  • Add CHANGELOG entry: - #592 : Copy all entries in `petsc_to_psydac` when `out` is given, instead of keeping stale values where the PETSc vector is zero, see #640
  • Remove psydac/linalg/topetsc_old.py and psydac/linalg/tests/bench_topetsc.py. I just kept them to benchmark before/after

@max-models max-models changed the title Vectorized approached to topetsc Vectorized version of topetsc Aug 11, 2026
@codacy-production

codacy-production Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 99 complexity · 18 duplication

Metric Results
Complexity 99
Duplication 18

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@yguclu

yguclu commented Aug 11, 2026

Copy link
Copy Markdown
Member

I noticed there are a lot of python loops in the topetsc functions, I think it's possible to improve the performance of these functions. I will try it out in this branch.

I thought those functions were accelerated with Pyccel..

@e-moral-sanchez

Copy link
Copy Markdown
Contributor

Hi, thanks for the PR. The conversion of matrices was pyccelized (within each block, see

for bc, bd in nonzero_block_indices:
if isinstance(mat, BlockLinearOperator):
mat_block = mat.blocks[bc][bd]
dnpts_block = dnpts_per_block_per_process[bd]
cnpts_block = cnpts_per_block_per_process[bc]
dshift_block = dindex_shift[bd]
cshift_block = cindex_shift[bc]
I,J,V,rowmap = toIJVrowmap(mat_block, bd, bc, I, J, V, rowmap, mat.domain, mat.codomain, dnpts_block, cnpts_block, dshift_block, cshift_block)
), but not the conversion of vectors.

@yguclu

yguclu commented Sep 4, 2026

Copy link
Copy Markdown
Member

Hi @max-models,

Thanks for this PR! What is its current status? Do you want us to review it, or should we wait for the "Ready for review" green light?

BTW, I have just merged devel into this branch, as I think it should use the latest version of PETSc.

@max-models

Copy link
Copy Markdown
Contributor Author

Hi @max-models,

Thanks for this PR! What is its current status? Do you want us to review it, or should we wait for the "Ready for review" green light?

BTW, I have just merged devel into this branch, as I think it should use the latest version of PETSc.

Hi, the status is that I unfortunately didn't have time to take a second look at it yet.

Once I have done that, I will mark it as ready for review :)

@max-models max-models changed the title Vectorized version of topetsc Vectorize vec_topetsc and petsc_to_psydac Oct 27, 2026
@max-models
max-models marked this pull request as ready for review October 27, 2026 16:04
@max-models
max-models requested review from e-moral-sanchez and removed request for e-moral-sanchez October 27, 2026 16:05
@e-moral-sanchez

Copy link
Copy Markdown
Contributor

Hi @max-models, is this PR ready for review?

@max-models

Copy link
Copy Markdown
Contributor Author

Hi @max-models, is this PR ready for review?

Yes!

@yguclu yguclu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@max-models Thanks a lot for this PR!

@e-moral-sanchez I cannot find a single call to the function psydac_to_petsc_global in the library, including the unit tests. Where is it used, then?

@e-moral-sanchez

Copy link
Copy Markdown
Contributor

@max-models Thanks a lot for this PR!

@e-moral-sanchez I cannot find a single call to the function psydac_to_petsc_global in the library, including the unit tests. Where is it used, then?

@yguclu , this function is used internally in vec_topetsc, for instance here:

i_g = psydac_to_petsc_global(vec.space, (b,), (i1_n,))

@yguclu

yguclu commented Nov 5, 2026

Copy link
Copy Markdown
Member

@max-models Thanks a lot for this PR!
@e-moral-sanchez I cannot find a single call to the function psydac_to_petsc_global in the library, including the unit tests. Where is it used, then?

@yguclu , this function is used internally in vec_topetsc, for instance here:

i_g = psydac_to_petsc_global(vec.space, (b,), (i1_n,))

After this PR, psydac_to_petsc_global and petsc_local_to_psydac are not used anymore, correct?

@yguclu

yguclu commented Nov 5, 2026

Copy link
Copy Markdown
Member

@max-models While reviewing, I also found that the new petsc_to_psydac fixes a bug in the current version when out is given (see #640, which I have just created).

I have taken the liberty to update the PR description accordingly; please have a look at the TODO list there. In addition:

  1. Please add a second CHANGELOG entry under "Changed" for the vectorization itself, e.g.: - #592 : Vectorize the conversion of vectors between PSYDAC and PETSc in `vec_topetsc` and `petsc_to_psydac`
  2. After this PR, the functions psydac_to_petsc_global and petsc_local_to_psydac in psydac.linalg.topetsc are no longer used anywhere in the library (they only appear in __all__ and in two comments). Should we remove them, or do you think they are useful as public helpers? @e-moral-sanchez what do you think?

@e-moral-sanchez

Copy link
Copy Markdown
Contributor

@max-models While reviewing, I also found that the new petsc_to_psydac fixes a bug in the current version when out is given (see #640, which I have just created).

I have taken the liberty to update the PR description accordingly; please have a look at the TODO list there. In addition:

1. Please add a second CHANGELOG entry under "Changed" for the vectorization itself, e.g.: `` - #592 : Vectorize the conversion of vectors between PSYDAC and PETSc in `vec_topetsc` and `petsc_to_psydac`  ``

2. After this PR, the functions `psydac_to_petsc_global` and `petsc_local_to_psydac` in `psydac.linalg.topetsc` are no longer used anywhere in the library (they only appear in `__all__` and in two comments). Should we remove them, or do you think they are useful as public helpers? @e-moral-sanchez what do you think?

I think that these functions are still useful to understand the index conversion, so in my opinion we should keep them.

@yguclu

yguclu commented Nov 9, 2026

Copy link
Copy Markdown
Member

@max-models While reviewing, I also found that the new petsc_to_psydac fixes a bug in the current version when out is given (see #640, which I have just created).
I have taken the liberty to update the PR description accordingly; please have a look at the TODO list there. In addition:

1. Please add a second CHANGELOG entry under "Changed" for the vectorization itself, e.g.: `` - #592 : Vectorize the conversion of vectors between PSYDAC and PETSc in `vec_topetsc` and `petsc_to_psydac`  ``

2. After this PR, the functions `psydac_to_petsc_global` and `petsc_local_to_psydac` in `psydac.linalg.topetsc` are no longer used anywhere in the library (they only appear in `__all__` and in two comments). Should we remove them, or do you think they are useful as public helpers? @e-moral-sanchez what do you think?

I think that these functions are still useful to understand the index conversion, so in my opinion we should keep them.

@e-moral-sanchez I understand what you mean, but these functions:

  1. Are not used any more;
  2. Are not tested;
  3. Are much slower than their vectorized counterparts;
  4. Are not part of the documentation.

So they are effectively dead code, and they may become obsolete (i.e. they may stop working properly). If we want to keep them, we need to have some unit tests for them at the very least

@e-moral-sanchez

e-moral-sanchez commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

@max-models While reviewing, I also found that the new petsc_to_psydac fixes a bug in the current version when out is given (see #640, which I have just created).
I have taken the liberty to update the PR description accordingly; please have a look at the TODO list there. In addition:

1. Please add a second CHANGELOG entry under "Changed" for the vectorization itself, e.g.: `` - #592 : Vectorize the conversion of vectors between PSYDAC and PETSc in `vec_topetsc` and `petsc_to_psydac`  ``

2. After this PR, the functions `psydac_to_petsc_global` and `petsc_local_to_psydac` in `psydac.linalg.topetsc` are no longer used anywhere in the library (they only appear in `__all__` and in two comments). Should we remove them, or do you think they are useful as public helpers? @e-moral-sanchez what do you think?

I think that these functions are still useful to understand the index conversion, so in my opinion we should keep them.

@e-moral-sanchez I understand what you mean, but these functions:

1. Are not used any more;

2. Are not tested;

3. Are much slower than their vectorized counterparts;

4. Are not part of the documentation.

So they are effectively dead code, and they may become obsolete (i.e. they may stop working properly). If we want to keep them, we need to have some unit tests for them at the very least

You are right, I agree that we can delete these functions since they won't be used anymore.

@codacy-production

codacy-production Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics -21 complexity · 0 duplication

Metric Results
Complexity -21
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@max-models

Copy link
Copy Markdown
Contributor Author

Thanks @yguclu @e-moral-sanchez, I made the following changes:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

petsc_to_psydac keeps stale values in out where the PETSc vector is zero

3 participants